<template>
  <div>
    <Header></Header>
    <div class="product-title">
      <!-- 标题 -->
      <ul class="title-list">
        <li v-for="(product, index) in titleList" :key="product.name" @click="setCurrentProduct(product)">
          <img :src="require('@/assets/images/HeadphonesSpeakers/' + product.image)" alt="Product Image" />
          <span>{{ product.name }}</span>
        </li>
      </ul>
    </div>

    <div class="product-container">
      <!-- 商品列表 -->
      <ul>
        <li v-for="(product, index) in currentProductList" :key="product.name"
          :class="{ 'last-item': (index + 1) % 4 === 0 }">
          <div class="product-item">
            <img :src="require('@/assets/images/HeadphonesSpeakers/' + product.image)" alt="Product Image" />

            <span class="sp1">{{ product.name }}</span>
            <span class="sp2">{{ product.introduce }}</span>
          </div>
        </li>
      </ul>
    </div>

    <!-- 底部 -->
    <Footer></Footer>
  </div>
</template>

<script>
// 引入底部组件
import Footer from "@/components/Footer.vue";
// 引入头部组件
import Header from "@/components/Header.vue";
// 引入json文件
let headphonesSpeakers = require("@/assets/data/headphonesSpeakers.json");
export default {
  components: {
    Footer, Header
  },
  name: "HeadphonesSpeakers",
  data() {
    return {
      titleList: headphonesSpeakers.titleList,
      currentProductList: [],
    };
  },
  methods: {
    setCurrentProduct(product) {
      this.currentProductList = headphonesSpeakers[product.productList];
    },
  },
  mounted() {
    this.setCurrentProduct(this.titleList[0]); // 默认显示耳机产品
  },
};
</script>
  
<style scoped>
.product-title {
  display: flex;
  text-align: center;
  margin: 70px 0 0 0;
}

.product-title .title-list {
  display: flex;
  justify-content: center;
  list-style: none;
  padding: 0;
  margin: 0 auto;
}

.product-title .title-list li {
  display: flex;
  flex-direction: column;
  align-items: center;
  cursor: pointer;
  margin-right: 20px;
  font-weight: bold;
  margin-bottom: 10px;
}

.product-title .title-list li.active {
  transform: scale(1.1);
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
}

.product-title .title-list img {
  width: 80px;
  height: 80px;
}

.product-container {
  margin: 0 auto;
  width: 1226px;
}

.product-container ul {
  display: flex;
  flex-wrap: wrap;
  list-style: none;
  padding: 0;
}

.product-container li {
  margin-bottom: 20px;
  flex-basis: calc(25% - 20px);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  /* 添加阴影效果 */
  border-radius: 10px;
  margin-right: 20px;
}

.product-container li.last-item {
  margin-right: 0;
}

.product-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  background-color: #fbfbfb;
  border-radius: 10px;
  height: 280px;
}

.product-item img {
  width: 200px;
  height: 200px;
  object-fit: cover;
  margin-bottom: 10px;
}

.product-item .sp1 {
  font-size: 24px;
  font-weight: bold;
  text-align: center;
}

.product-item .sp2 {
  font-size: 14px;
  font-weight: bold;
  text-align: center;
}

.product-title .title-list li:hover img,
.product-container li:hover img {
  transform: scale(1.1);
  transition: all 0.5s ease;
}

.product-title .title-list li img,
.product-container li img {
  transition: all 0.5s ease;
}
</style>